package com.dd.ent.car.ui.note;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.User;
import com.dd.ent.car.common.bean.note.Note;
import com.dd.ent.car.common.http.ImageLoaderUtil;
import com.dd.ent.car.common.util.StringUtils;
import com.dd.ent.car.common.xrichtext.ui.NoteActivity;
import com.pp.ent.Cherry;
import com.pp.ent.core.manage.UIHandler;

import java.util.List;

import static com.dd.ent.car.common.config.UIConfig.NOTIFY_NOTE_UPDATE_COMMENT_COUNT;

/**
 * Created by dengqu on 2017/2/23.
 */

public class NoteItemShortView extends FrameLayout {
    private ImageView mPoster;
    private TextView mTexTitle;
    private TextView mTextUserName;
    private TextView mTextCommentCount;
    private int position;
    private Note note;

    public NoteItemShortView(Context context) {
        super(context);
        init(context);
    }

    public NoteItemShortView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.item_list_short_forum, this, true);
        mPoster = (ImageView) root.findViewById(R.id.item_icon);
        mTexTitle = (TextView) root.findViewById(R.id.item_title);
        mTextUserName = (TextView) root.findViewById(R.id.tex_user_name);
        mTextCommentCount = (TextView) root.findViewById(R.id.tex_comment_count);
        root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                NoteActivity.startSelf(getContext(), NoteActivity.From.FORUM, note);
            }
        });
    }

    public void refreshData(int position, Note item) {
        this.position = position;
        this.note = item;
        mPoster.setVisibility(VISIBLE);
        if (item != null) {
            mTexTitle.setText(item.getTitle());
            mTextCommentCount.setText(item.getCommentCount() + "回帖");
            User user = item.getUser();
            if (user != null && !TextUtils.isEmpty(user.getUserName())) {
                mTextUserName.setText(user.getUserName());
            }
            List<String> textList = StringUtils.cutStringByImgTag(item.getContent());
            for (int i = 0; i < textList.size(); i++) {
                String text = textList.get(i);
                if (text.contains("<img") && text.contains("src=")) {
                    String imagePath = StringUtils.getImgSrc(text);
                    ImageLoaderUtil.displayPosterImage(imagePath, mPoster);
                    return;
                }
            }
        }
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Cherry.injectObject(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Cherry.remove(this);
    }

    @UIHandler(NOTIFY_NOTE_UPDATE_COMMENT_COUNT)
    public void onNoteUpdateCommentCount(Note note) {
        if (note != null && this.note != null) {
            if (TextUtils.equals(note.getUuid(), this.note.getUuid())) {
                refreshData(position, note);
            }
        }
    }

}
